<?php

namespace App\Providers;

use FFMpeg\FFMpeg;
use FFMpeg\FFProbe;
use Illuminate\Support\ServiceProvider;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // sql语句打印监听
        $this->registerListenSql();
        // 数据表监听绑定
        $this->registerModelListen();
        // 自定义验证规则
        $this->registerValidator();
        // 注册ffmpeg
        $this->registerFFmpeg();
    }
    /**
     * 监听model修改
     * @name: registerModelListen
     * @author: zhuxuesong
     * @datetime: 2022-02-11 16:33
     */
    private function registerModelListen()
    {
//        Admin::observe(AdminObserver::class);
    }

    /**
     * sql语句监听
     */
    private function registerListenSql()
    {
        \DB::listen(function ($query) {
            $uri = request()->getPathInfo();
            $sql = str_replace("?", "'%s'", $query->sql);
            try {
                $log = vsprintf($sql, $query->bindings);
                if (stripos($log, '`api_logs`') !== false) return;
                \Log::info(' execution time: ' . $query->time . 'ms; ' . $log . "\n\t");
            } catch (\Exception $e) {
                \Log::info($sql);
            }
        });
    }

    /**
     * 注册验证类型
     */
    private function registerValidator()
    {
        /**
         * 手机号验证
         */
        Validator::extend('phone', function ($attribute, $value, $parameters, $validator) {
            $reg = "/(^1[123456789]{1}\d{9}$)/";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });
        /**
         * 电话验证
         */
        Validator::extend('contact_tel', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^(([0|4]\d{2,3}-?\d{7,8})|(1[123456789]\d{9}))$/";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });
        /**
         * 验证密码
         */
        Validator::extend('pwd', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^[a-zA-Z\d]{6,20}$/";
            $reg = "/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z!@#$%^&*()-=_+]{6,20}$/";
            if (preg_match($reg, $value)) {
                \Log::info("通过：" . $value);
                return true;
            } else {
                \Log::info("失败：" . $value);
                return false;
            }
        });
        /**
         * 验证码
         */
        Validator::extend('sms_verify_code', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^[0-9]{6}$/";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });

        /**
         * 验证车牌号
         */
        Validator::extend('car_num', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/";
            return true;
            //            if (preg_match($reg, $value)) {
//                return true;
//            } else {
//                return false;
//            }
        });

        /**
         * 验证身份证号
         */
        Validator::extend('id_card_num', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });
        /**
         * 验证姓名
         */
        Validator::extend('real_name', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^[\x{4e00}-\x{9fa5}]{2,20}$/u";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });
        /**
         * 验证统一社会信用代码
         */
        Validator::extend('credit_code', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^[A-Za-z0-9]{18}$/";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });
        /**
         * 验证银行卡号
         */
        Validator::extend('bank_card_no', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^[0-9]{5,50}$/";
            if (preg_match($reg, $value)) {
                return true;
            } else {
                return false;
            }
        });
    }

    private function registerFFmpeg()
    {
        $this->app->singleton('ffmpeg', function ($app) {
            if (PHP_OS === "WINNT" && is_local()) {
                return FFMpeg::create([
                    'ffmpeg.binaries'  => 'D:\ffmpeg\bin\ffmpeg.exe',
                    'ffprobe.binaries' => 'D:\ffmpeg\bin\ffprobe.exe',
                ]);
            } else {
                return FFMpeg::create([
                    'ffmpeg.binaries'  => '/www/server/ffmpeg/ffmpeg-6.1/ffmpeg',
                    'ffprobe.binaries' => '/www/server/ffmpeg/ffmpeg-6.1/ffprobe',
                ]);
            }
        });

        $this->app->singleton('ffprobe', function ($app) {
            if (PHP_OS === 'WINNT' && is_local()) {
                return FFProbe::create([
                    'ffprobe.binaries' => 'D:\ffmpeg\bin\ffprobe.exe',
                ]);
            } else {
                return FFProbe::create([
                    'ffprobe.binaries' => '/usr/local/ffmpeg/ffprobe',
                ]);
            }
        });
    }
}
